package com.fc.test.api;

import com.fc.test.model.auto.TAbbJquancheng;
import com.fc.test.model.auto.TAbbQuancheng;
import com.fc.test.service.TAbbJquanchengService;
import com.fc.test.service.TAbbQuanchengService;
import com.fc.test.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("apiQuanchengController")
@Api(value="全城任务")
public class ApiQuanchengController {

    @Autowired
    private TAbbQuanchengService tAbbQuanchengService;

    @Autowired
    private TAbbJquanchengService tAbbJquanchengService;

    @ResponseBody
    @PostMapping(value = "getQuanchengByPage",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "分页查询全城任务")
    public String getQuanchengByPage(
            @ApiParam(value = "页码") @RequestParam(value = "pageIndex") int  pageIndex,
            @ApiParam(value = "用户id") @RequestParam(value = "memberId") String  memberId
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<TAbbQuancheng> list = tAbbQuanchengService.getQuanchengByPage(pageIndex,memberId);
        resMap.put("list",list);
        resMap.put("success",true);
        if(list.size()>0){
            resMap.put("msg","获取全城任务成功");
        }else{
            resMap.put("msg","已经到底了");
        }
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "getQuanchengDetail",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "全城任务详情")
    public String getQuanchengDetail(
            @ApiParam(value = "全城任务id") @RequestParam(value = "id") String id,
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid
    ){
        Map<String,Object> resMap = new HashMap<>();
        resMap = tAbbQuanchengService.getQuanchengDetail(id,userid);
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "getQcRecords",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "任务记录")
    public String getQcRecords(
            @ApiParam(value = "用户id") @RequestParam(value = "memberId") String memberId,
            @ApiParam(value = "页码") @RequestParam(value = "page") int page,
            @ApiParam(value = "状态(1,待提交 2，待审核 3，审核未通过 4，已完成)") @RequestParam(value = "status") int status
    ){
        Map<String,Object> resMap = new HashMap<>();
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("memberId",memberId);
        paramMap.put("page",page);
        paramMap.put("status",status);
        List<TAbbJquancheng> list = tAbbJquanchengService.getQcRecords(paramMap);
        resMap.put("success",true);
        resMap.put("msg","获取任务记录列表成功");
        resMap.put("list",list);
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "deleteQcRecords",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "关闭全城任务")
    public String deleteQcRecords(
            @ApiParam(value = "任务领取id") @RequestParam(value = "id") String id
    ){
        Map<String,Object> resMap = new HashMap<>();
        int flag = tAbbJquanchengService.deleteQcRecords(id);
        if(flag>0){
            resMap.put("success",true);
            resMap.put("msg","关闭全城任务成功");
        }else{
            resMap.put("success",false);
            resMap.put("msg","关闭全城任务失败");
        }
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "receiveQuancheng",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "领取全城任务")
    public String  receiveQuancheng(
            @ApiParam(value = "任务id") @RequestParam(value = "qcid") String qcid,
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid
    ){
        Map<String,Object> resMap = new HashMap<>();
        resMap= tAbbJquanchengService.receiveQuancheng(qcid,userid);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value = "submitQuancheng",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "提交全城任务")
    public String submitQuancheng(
            @ApiParam(value = "任务领取id") @RequestParam(value = "jdId") String jdId,
            @ApiParam(value = "任务回复图片") @RequestParam(value = "images") String images
    ){
        Map<String,Object> resMap = new HashMap<>();
        resMap = tAbbJquanchengService.submitQuancheng(jdId,images);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value = "releaseQuancheng",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "发布全城任务")
    public String releaseQuancheng(
            @ApiParam(value = "用户id") @RequestParam(value = "memberid") String memberid,
            @ApiParam(value = "标题") @RequestParam(value = "title") String title,
            @ApiParam(value = "任务介绍") @RequestParam(value = "introduction") String introduction,
            @ApiParam(value = "任务图片") @RequestParam(value = "images") String images,
            @ApiParam(value = "任务总条数") @RequestParam(value = "number") int number
    ){
        Map<String,Object> resMap = new HashMap<>();
        TAbbQuancheng tAbbQuancheng = new TAbbQuancheng();
        tAbbQuancheng.setMemberid(memberid);
        tAbbQuancheng.setTitle(title);
        tAbbQuancheng.setIntroduction(introduction);
        tAbbQuancheng.setImages(images);
        tAbbQuancheng.setNumber(number);
        resMap = tAbbQuanchengService.releaseQuancheng(tAbbQuancheng);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value = "getMyQuanchengList",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "我发布的全城任务")
    public String getMyQuanchengList(
            @ApiParam(value = "用户id") @RequestParam(value = "page") int page,
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<TAbbQuancheng> list = tAbbQuanchengService.getMyQuanchengList(page,userid);
        resMap.put("list",list);
        resMap.put("success",true);
        if(list.size()>0){
            resMap.put("msg","获取我发布的全城任务成功");
        }else{
            resMap.put("msg","已经到底了");
        }
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "getQuanchengPrice",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "全城任务价格")
    public String getQuanchengPrice(){
        Map<String,Object> resMap = tAbbQuanchengService.getQuanchengPrice();
        return JsonUtil.jsonMapStr(resMap);
    }

}
